<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/2/24
 * Time: 16:56
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\GoodsModel;
use SDXApi\Repositories\BuyNowGoodsRepository;

/**
 * Class HomeTemplateBuyNowService
 * @package SDXApi\Service
 */
class HomeTemplateBuyNowService extends TemplateAbstract
{
	/**
	 * @var GoodsModel
	 */
	protected $goodsModel;

	/**
	 * @var BuyNowGoodsRepository
	 */
	protected $buyNowGoodsRepository;

	/**
	 * HomeTemplateBuyNowService constructor.
	 */
	public function __construct()
	{
		$this->goodsModel = new GoodsModel();
		$this->buyNowGoodsRepository = new BuyNowGoodsRepository();
	}

	/**
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
		$data = $this->findGoodsBuyNowList($userId, $wid);

		if (empty($data)) {
			return false;
		}

//		过滤已过期秒杀商品
		$goods = $this->filtrateBuyNowGoods($data);
		$groupGoods = self::checkBuyNowGoodsDateGroup($goods);

		$groupGoods = array_merge($groupGoods);

		if (empty($groupGoods)) {
			$groupGoods = array();
		}

		$groupGoods = self::checkGroupDataSortAsc($groupGoods, 'time_key');

		$buyNowGoodsListNow = self::checkDataSortAsc($goods, 'buy_now_end_time', 3);
		$buyNowGoodsListNext = self::checkDataSortAsc($goods, 'buy_now_start_time', 2);
//        $buyNowGoodsListNumber = self::checkDataSortAsc($goods, 'buy_now_start_time', 4);

		if (empty($buyNowGoodsListNow)) {
			$buyNowGoodsListNow = array();
		}

		if (empty($buyNowGoodsListNext)) {
			$buyNowGoodsListNext = array();
		}

//		$buyNowGoodsList = array_merge($buyNowGoodsListNow, $buyNowGoodsListNext, $buyNowGoodsListNumber);
        $buyNowGoodsList = array_merge($buyNowGoodsListNow, $buyNowGoodsListNext);

		$template['buy_now_group'] = $groupGoods;
		$template['buy_now_list'] = $buyNowGoodsList;
		return $template;
	}

    /**
     * @author Jerry
     * @param int $uid
     * @param int $wid
     * @return array|bool
     */
	public function getBuyNowGroupGoods($uid = 0, $wid = 8)
    {
        $data = $this->findGoodsBuyNowList($uid, $wid);

        if (empty($data)) {
            return false;
        }

        $goods = $this->filtrateBuyNowGoods($data);

        $groupGoods = self::checkBuyNowGoodsDateGroup($goods);
        $groupGoods = array_merge($groupGoods);

        if (empty($groupGoods)) {
            return false;
        }

        return $groupGoods;
    }

	/**
	 * 获取秒杀商品
	 * Created by Jerry
	 * @param int $userId
	 * @param int $wid
	 * @return mixed
	 */
	protected function findGoodsBuyNowList($userId = 0, $wid = 8)
	{
		$time = gmtime();
		$getTime = date('Y-m-d', $time);
		$startTime = strtotime($getTime) - 28800; //开始时间
		$endTime = strtotime('+1 days', $startTime);  //结束时间

//		设置
		$this->buyNowGoodsRepository->userId = $userId;
		$this->buyNowGoodsRepository->wid = $wid;
		$data = $this->findGoodsBuyNowListByPromoteTime($startTime, $endTime, $wid);
		$goods = $this->buyNowGoodsRepository->formatBuyNowGoods($data);
		return $goods;
	}

	/**
	 * 过滤过期商品
	 * Created by Jerry
	 * @param array $data
	 * @return array|bool
	 */
	protected function filtrateBuyNowGoods($data = array())
	{
		if (empty($data)) {
			return false;
		}

		$goods = array();
		foreach ($data as $key => $value) {
			$arr = $this->checkBuyNowGoodsDataFormat($value);

			if ($arr['buy_now_status'] == 1 || $arr['buy_now_status'] == 0) {
				continue;
			}
			$goods[$key] = $arr;
		}

		return $goods;
	}

    /**
     * @author Jerry
     * @param $startTime
     * @param $endTime
     * @param int $wid
     * @return mixed
     */
	protected function findGoodsBuyNowListByPromoteTime($startTime, $endTime, $wid = 8)
	{
		return $this->goodsModel->findGoodsBuyNowListByPromoteTime($startTime, $endTime, $wid);
	}

	/**
	 * Created by Jerry
	 * @param $goods
	 * @return bool|mixed
	 */
	protected function checkBuyNowGoodsDataFormat($goods)
	{
		if (empty($goods)) {
			return false;
		}

		return $this->buyNowGoodsRepository->formatBuyNowGoodsData($goods);
	}

	/**
	 * Created by Jerry
	 * @param $data
	 * @return array|bool
	 */
	static protected function checkBuyNowGoodsDateGroup($data)
	{
		if (empty($data)) {
			return false;
		}

		$time = time();

		$goods = array();
		foreach ($data as $key => $value) {

		    if ($value['buy_now_status'] == 4) {
		        continue;
            }

			$key = $value['buy_now_time_key'];
			unset($value['goods_brief']);
			$goods[$key]['start_time_sec'] = $value['buy_now_start_time'] > $time ? $value['buy_now_start_time'] - time() : 0;
            $goods[$key]['end_time_sec'] = $value['buy_now_end_time'] > $time && $value['buy_now_start_time'] < time() ? $value['buy_now_end_time'] - $time : 0;
			$goods[$key]['date_key'] = $value['buy_now_date_key'];
			$goods[$key]['time_key'] = strtotime($value['buy_now_date_key']);
			$goods[$key]['start_date'] = $value['buy_now_start_date'];
            $goods[$key]['end_date'] = $value['buy_now_end_date'];
			$goods[$key]['buy_now_status'] = $value['buy_now_status'];
			unset($value['buy_now_date_key'], $value['buy_now_time_key']);
			$goods[$key]['goods_list'][] = $value;
		}

		return $goods;
	}

	/**
	 * Created by Jerry
	 * @param $data
	 * @param string $sortKey
	 * @param int $status
	 * @return array
	 */
	static protected function checkDataSortAsc($data, $sortKey = 'sort', $status = 3)
	{
		$arr = array();
		$sort = array();
		foreach ($data as $key => $value) {
			if ($value['buy_now_status'] == $status) {
				$arr[] = $value;
				$sort[] = $value[$sortKey];
			}
		}

		array_multisort($sort, SORT_ASC, $arr);

		return $arr;
	}

    /**
     * @author Jerry
     * @param $data
     * @param string $sortKey
     * @return mixed
     */
    static protected function checkGroupDataSortAsc($data, $sortKey = 'sort')
    {
        $sort = array();
        foreach ($data as $key => $value) {
            $sort[] = $value[$sortKey];
        }

        array_multisort($sort, SORT_ASC, $data);

        return $data;
    }
}